package com.chw.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chw.common.diy.CommonData;
import com.chw.common.diy.PassExction;
import com.chw.common.exception.NameException;
import com.chw.common.exception.ParamExcption;
import com.chw.user.entity.po.User;
import com.chw.user.mapper.UserMapper;
import com.chw.user.service.UserService;
import com.chw.util.JWT;
import com.chw.util.email.ToEmail;
import com.chw.util.md5.PasswordMd5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    //注入redis
    private RedisTemplate redisTemplate;

    @Override
    public List<User> queryAllData() {
        List<User> list = userMapper.selectList(null);
        return list;
    }

    //比较邮箱是否存在
    @Override
    public Integer equalsEmail(String email) throws Exception {
        if (StringUtils.isEmpty(email)) {
            throw new ParamExcption();
        }
        String regExl = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        boolean matches = email.matches(regExl);
        if (!matches) {
            throw new ParamExcption();//邮箱格式不正确
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("email", email);
        List<User> list = userMapper.selectList(queryWrapper);
        if (list.size() != 0) {
            return 0;//邮箱已存在
        }
        Object o = redisTemplate.opsForValue().get(email + CommonData.PRO);
        if (o != null) {
            return 2;//已发过验证码
        }
        String code = getCode();
        ToEmail.sendMail(email, code, CommonData.FEIHU);
        //把验证码放入redis
        redisTemplate.opsForValue().set(email + CommonData.PRO, code, 60, TimeUnit.SECONDS);

        return 1;//发送成功
    }

    @Override
    public Integer createData(User user) {
        if (user.getName() == null || user.getPassword() == null) {
            throw new ParamExcption();
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", user.getName());
        List<User> list = userMapper.selectList(queryWrapper);
        if (list.size() != 0) {
            return 0;//用户名已存在
        }
        user.setPassword(PasswordMd5.stringToMD5(PasswordMd5.stringToMD5(user.getPassword()) + user.getName()));
        user.setCreateDate(new Date());
        userMapper.insert(user);
        return  1;
    }


    @Override
    public Integer validatecode(String email,String code) {
        Object o = redisTemplate.opsForValue().get(email + CommonData.PRO);
        if (o==null){
            return 2;//验证码已过期
        }
        if (o.equals(code)){
            return 1;//验证成功
        }
        return 0;//验证失败
    }

    @Override
    public Object login(User user) {
        if (user.getName()==null || user.getPassword()==null){
            throw  new ParamExcption();
        }
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("name",user.getName());
        User user1 =  userMapper.selectOne(queryWrapper);
        if (user1==null){
            throw new NameException();
        }
        String s = PasswordMd5.stringToMD5(PasswordMd5.stringToMD5(user.getPassword()) + user.getName());

        if (!s.equals(user1.getPassword())){
            throw  new PassExction();
        }
        String sign = JWT.sign(user1, 60 * 60 * 24);
        return sign;
    }

    //生成时间戳获取后四位作为验证码
    public  static  String getCode(){
       String dateC= System.currentTimeMillis()+"";
        System.out.println(dateC);
        return dateC.substring(dateC.length()-4,dateC.length());
    }

    public static void main(String[] args) {
        System.out.println( getCode());
    }
}
